package com.yuan.kafkastudy.SocketTest;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@SpringBootTest
public class SendMessage {
    @Test
    public void testSendMessage() {
        String message = "hello";
        byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
        int length = bytes.length;
        try (Socket socket = new Socket("127.0.0.1",19001);
             DataOutputStream out = new DataOutputStream(socket.getOutputStream());){
            // 造 50 MB 数据
//            byte[] payload = new byte[50 * 1024 * 1024];
//            Arrays.fill(payload, (byte) 'A');
//
//            out.writeInt(payload.length); // 4 字节长度
//            out.write(payload);           // 50 MB 正文
//            out.flush();
//            System.out.println("50 MB 已发出");
            for (int i = 0; i < 50; i++) {
                out.writeInt(length);
                out.write(bytes);
            }
            Thread.sleep(1000000);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
